home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADA / pickdepth_procs.adb < prev    next >
Encoding:
Text File  |  1998-08-12  |  5.4 KB  |  167 lines

  1. --
  2. --  (c) Copyright 1993,1994,1995,1996 Silicon Graphics, Inc.
  3. --  ALL RIGHTS RESERVED
  4. --  Permission to use, copy, modify, and distribute this software for
  5. --  any purpose and without fee is hereby granted, provided that the above
  6. --  copyright notice appear in all copies and that both the copyright notice
  7. --  and this permission notice appear in supporting documentation, and that
  8. --  the name of Silicon Graphics, Inc. not be used in advertising
  9. --  or publicity pertaining to distribution of the software without specific,
  10. --  written prior permission.
  11. --
  12. --  THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13. --  AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14. --  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15. --  FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16. --  GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17. --  SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18. --  KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19. --  LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20. --  THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21. --  ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22. --  ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23. --  POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24. --
  25. --  US Government Users Restricted Rights
  26. --  Use, duplication, or disclosure by the Government is subject to
  27. --  restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28. --  (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29. --  clause at DFARS 252.227-7013 and/or in similar or successor
  30. --  clauses in the FAR or the DOD or NASA FAR Supplement.
  31. --  Unpublished-- rights reserved under the copyright laws of the
  32. --  United States.  Contractor/manufacturer is Silicon Graphics,
  33. --  Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34. --
  35. --  OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36. --
  37.  
  38. with GL; use GL;
  39. with GLU; use GLU;
  40. with GLUT; use GLUT;
  41. with Text_IO;
  42.  
  43. package body PickDepth_Procs is
  44.    package tio renames Text_IO;
  45.  
  46.    procedure DoInit is
  47.    begin
  48.       glClearColor (0.0, 0.0, 0.0, 0.0);
  49.       glDepthFunc (GL_LESS);
  50.       glEnable (GL_DEPTH_TEST);
  51.       glShadeModel (GL_FLAT);
  52.       glDepthRange (0.0, 1.0);
  53.    end DoInit;
  54.  
  55.    procedure DrawRects (mode : RenderingMode) is
  56.    begin
  57.       if mode = GL_SELECT then glLoadName (1); end if;
  58.       glBegin (GL_QUADS);
  59.       glColor3f (1.0, 1.0, 0.0);
  60.       glVertex3i (2, 0, 0);
  61.       glVertex3i (2, 6, 0);
  62.       glVertex3i (6, 6, 0);
  63.       glVertex3i (6, 0, 0);
  64.       glEnd;
  65.  
  66.       if mode = GL_SELECT then glLoadName (2); end if;
  67.       glBegin (GL_QUADS);
  68.       glColor3f (0.0, 1.0, 1.0);
  69.       glVertex3i (3, 2, -1);
  70.       glVertex3i (3, 8, -1);
  71.       glVertex3i (8, 8, -1);
  72.       glVertex3i (8, 2, -1);
  73.       glEnd;
  74.  
  75.       if mode = GL_SELECT then glLoadName (3); end if;
  76.       glBegin (GL_QUADS);
  77.       glColor3f (1.0, 0.0, 1.0);
  78.       glVertex3i (0, 2, -2);
  79.       glVertex3i (0, 7, -2);
  80.       glVertex3i (5, 7, -2);
  81.       glVertex3i (5, 2, -2);
  82.       glEnd;
  83.    end DrawRects;
  84.  
  85.    type int_ar is array (Integer range <>) of aliased GLuint;
  86.  
  87.    procedure ProcessHits (hits : GLint; buffer : in int_ar) is
  88.       j : Integer := buffer'First;
  89.    begin
  90.       tio.Put_Line ("Hits = " & GLint'Image (hits));
  91.  
  92.       if hits /= 0 then
  93.          for i in
  94.             Integer (buffer'First) ..
  95.                Integer (buffer'First + Integer (hits) - 1)
  96.          loop
  97.             tio.Put_Line (" number of names for hit = " &
  98.                GLuint'Image (buffer (j)));
  99.             j := j + 1;
  100.             tio.Put (" z1 is " & GLuint'Image (buffer (j)));
  101.             j := j + 1;
  102.             tio.Put ("; z2 is " & GLuint'Image (buffer (j)));
  103.             j := j + 1;
  104.             tio.New_Line;
  105.             tio.Put ("  names:");
  106.  
  107.             for k in 1 .. Integer (buffer (buffer'First)) loop
  108.                tio.Put (" " & GLuint'Image (buffer (j)));
  109.                j := j + 1;
  110.             end loop;
  111.             tio.New_Line;
  112.          end loop;
  113.       end if;
  114.    end ProcessHits;
  115.  
  116.    BUFSIZE : constant := 512;
  117.  
  118.    procedure PickRects (btn : Integer; state: Integer; x, y: Integer) is
  119.       selectBuf : array (1 .. BUFSIZE) of aliased GLuint;
  120.       hits : GLint;
  121.       viewport : array (0 .. 3) of aliased GLint;
  122.    begin
  123.     if state = GLUT_LEFT_BUTTON then
  124.      if state = GLUT_DOWN then
  125.       glGetIntegerv (GL_VIEWPORT, viewport (0)'Access);
  126.  
  127.       glSelectBuffer (BUFSIZE, selectBuf (1)'Access);
  128.       hits := glRenderMode (GL_SELECT);
  129.  
  130.       glInitNames;
  131.       glPushName (-1);
  132.  
  133.       glMatrixMode (GL_PROJECTION);
  134.       glPushMatrix;
  135.       glLoadIdentity;
  136.       gluPickMatrix (GLdouble (x), GLdouble (viewport (3) - GLint(y)),
  137.          5.0, 5.0, viewport (0)'Access);
  138.       glOrtho (0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
  139.       DrawRects (GL_SELECT);
  140.       glPopMatrix;
  141.       glFlush;
  142.  
  143.       hits := glRenderMode (GL_RENDER);
  144.       ProcessHits (hits, int_ar (selectBuf));
  145.      end if;
  146.     end if;
  147.    end PickRects;
  148.  
  149.    procedure DoDisplay is
  150.    begin
  151.       glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  152.       DrawRects (GL_RENDER);
  153.       glFlush;
  154.    end DoDisplay;
  155.  
  156.  
  157.    procedure ReshapeCallback (w : Integer; h : Integer) is
  158.    begin
  159.       glViewport (0, 0, GLsizei(w), GLsizei(h));
  160.       glMatrixMode (GL_PROJECTION);
  161.       glLoadIdentity;
  162.       glOrtho (0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
  163.       glMatrixMode (GL_MODELVIEW);
  164.       glLoadIdentity;
  165.    end ReshapeCallback;
  166. end PickDepth_Procs;
  167.